package com.training.xml;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlDemo02 {

	public static void main(String[] args) throws Exception {
		String fileName = "E:\\JavaWork\\java-io\\src\\com\\training\\resources\\cars.xml";

		SAXReader in = new SAXReader();
		Document doc = in.read(new FileReader(fileName));

		Element root = doc.getRootElement();
		Element element = root.addElement("car");
		
		element.addAttribute("brand", "BMW");
		element.addAttribute("color", "red");
		
		OutputFormat format = OutputFormat.createCompactFormat();
		
		format.setEncoding("utf-8");
		XMLWriter out = new XMLWriter(new FileWriter(fileName),format);
		out.write(doc);
		out.close();
	}

}
